home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / Venus / window.h < prev   
Encoding:
C/C++ Source or Header  |  1996-06-22  |  7.6 KB  |  199 lines  |  [TEXT/ALFA]

  1. /*
  2.  *************************************************************************
  3.  *
  4.  *                        A Basic Window class
  5.  *
  6.  *         A generic simple window that can either show up by itself
  7.  *    (and be dragged around on the screen and closed by clicking a
  8.  *                            "go-away" button)
  9.  *    or appear as a special item in a dialog
  10.  *
  11.  * Class ScreenWindow provides a very generic event and control processing
  12.  * that is common to regular windows with controls, modal and modeless
  13.  * dialogs. The basic functionality is to route events to appropriate
  14.  * handlers, process close/destroy/etc events, and handle controls.
  15.  * The class ScreenWindow provides only basic event handling, and it's
  16.  * abstract: you have to create your own class on the base of ScreenWindow
  17.  * and define the draw() function that is called on Update event to
  18.  * paint/repaint the window. You may want to redefine couple of other
  19.  * vanilla handlers (say, for control items (buttons, etc)).
  20.  *
  21.  * When you create the window using explicit specification of the rectangle, title, etc,
  22.  * the window would be invisible right after the creation. It's better to set up
  23.  * invisible (or hidden) attribute in the window resource if you create
  24.  * a window from the resource template. That way you still can do some
  25.  * rearrangements (say, create child windows) before the event processing
  26.  * starts.
  27.  *
  28.  * To show the window (with all the controls, children etc) and start
  29.  * processing of events, call show(). If you need to wait until the window
  30.  * is closed (after the user clicked some button or clicked at the go-away
  31.  * region, or selected "Close" from the menu, or something) call the
  32.  * serve_to_death() function. It returns only when all the opened windows
  33.  * are closed.
  34.  *
  35.  * $Id: Window.h,v 2.2 1994/11/07 20:33:30 oleg Exp oleg $
  36.  *
  37.  ***********************************************************************
  38.  */
  39.  
  40. #ifndef __GNUC__
  41. #pragma once
  42. #else
  43. #pragma interface
  44. #endif
  45.  
  46. #ifndef _Window_h
  47. #define _Window_h 1
  48.  
  49. #include <QDOffScreen.h>
  50.  
  51. /*
  52.  *----------------------------------------------------------------------
  53.  *                Service functions
  54.  */
  55.  
  56. class IMAGE;                // Opaque class
  57.  
  58. class ScreenRect : public Rect
  59. {
  60. public:
  61.   ScreenRect(const Rect& rect) : Rect(rect) {}
  62.   ScreenRect(const IMAGE& image);
  63. //  ScreenRect(const rowcol& heightwidth);
  64.                     // Create a rectangle of given height/width
  65.                     // positioned at a given point
  66. //  ScreenRect(const rowcol& origin, const rowcol& heightwidth);
  67.   ScreenRect& operator += (const int offset);
  68.   operator Rect * (void)                 { return this; }
  69.   int q_width(void)  const              { return right >= left ? right - left : left - right; }
  70.   int q_height(void) const              { return bottom >= top ? bottom - top : top - bottom; }
  71.                                 // Give a STATIC string representation of
  72.                                 // the rectangle
  73. //  operator const char * (void) const;
  74.   void print(const char * title = "") const;
  75. };
  76.  
  77.  
  78.                 // Make it easy setting and resetting
  79.                 // the current GrafPtr
  80. class SetNewGrafPtr
  81. {
  82.   GrafPtr old_port;
  83. public:
  84.                     // Switch to an off-screen grafworld
  85.   SetNewGrafPtr(const GrafPtr new_grafport) { GetPort(&old_port); SetPort(new_grafport); }
  86.                     // Restore the original grafworld
  87.  ~SetNewGrafPtr(void) { SetPort(old_port); }
  88. };
  89.  
  90. /*
  91.  *----------------------------------------------------------------------
  92.  *    A generic simple window that can be dragged around the screen
  93.  *        and closed by clicking a "go-away" button
  94.  * No other events are handled, redefine the event handler if necessary.
  95.  */
  96.  
  97. class ScreenWindow
  98. {
  99.   friend class ModelessDialog;
  100.   
  101.   WindowPtr this_window;
  102. //  Boolean Not_canceled;         // set to FALSE if Cancel button was pressed
  103.                                 // or the window was forcibly closed
  104.  
  105. //  static long universal_handler(WINDOW win, EVENT *ep);
  106. //  static int no_opened_windows;
  107.     
  108.   virtual Boolean handle_mouse_down(const EventRecord& the_event, WindowPtr where_window, short window_part);
  109.   virtual Boolean handle_key_down(const EventRecord& the_event);    // Handles key_down & auto_key events
  110.   void update(void);
  111.   virtual void draw(void) = 0;            // It is this function that really draws smth
  112.     
  113.   ScreenWindow(void) : this_window(nil) {}    // Constructor that does nothing, and initializes
  114.                                               // nothing. Trick to emulate a virtual constructor
  115. protected:
  116.   WindowPtr our_window(void) const     { return this_window; }
  117.  
  118.                                 // Draw w/o waiting for an UPDATE event:
  119.                                 // Use for animation
  120.   void draw_immediately(void)   { draw(); /*?update()?? */}
  121.  
  122.   virtual void destroy_it(void);            // I wouldn't've needed this is destructor had been
  123.                                               // virtual. Right now, CW 6.0 accepts attribute virtual
  124.                                               // for the destructor, but doesn't override
  125.                                               // destructors as it does for virtual functions.
  126.                                               // Oh, well, another kludge
  127.     
  128. public:
  129.   ScreenWindow(ScreenRect rect, const char * title);
  130.   ScreenWindow(const short resource_id);        // Create a window from a resource template
  131.   ~ScreenWindow(void)                    { destroy_it(); }
  132.   void refresh(void);
  133.                                 // Serve opened windows until all get closed
  134. //  static void serve_to_death(void);
  135.  
  136.                                 // Call this to start the service
  137.   void show(const Boolean onoff=TRUE) const  { ShowHide(this_window, onoff); }
  138. //  virtual void cancel(void);            // Close the window
  139.  // operator Boolean (void) const         { return Not_canceled; }
  140.   //Boolean is_this_window(const WindowPtr some_window) const { return some_window == this_window; }
  141.   //Boolean operator == (const WindowPtr some_window) const { return is_this_window(some_window); }
  142.  
  143.                                   // ScreenWindow event dispatch entry points
  144.                                   // Return FALSE if this object doesn't want any
  145.                                   // more events
  146.   virtual Boolean handle_null_event(const long event_time);
  147.   virtual Boolean handle_event(const EventRecord& the_event);
  148. };
  149.  
  150. /*
  151.  *----------------------------------------------------------------------
  152.  *                An off-screen pixel buffer for faster drawing
  153.  */
  154.  
  155. class OffScreenBuffer
  156. {
  157.   GWorldPtr graf_world;                // (Offscreen) graphical world that contains the picture
  158.  
  159. protected:
  160.   PixMapHandle pixmap;                // Pixmap for the offscreen world
  161.   int _height;                        // Dimensions of the pixmap (precomputed for
  162.   int _width;                        // easy reference
  163.   int _bytes_per_row;                // Note, bytes_per_row >= width (and generally, not equal)
  164.  
  165.                                       // Draw the offscreen buffer on screen
  166.   void draw(const Rect& where_rect, const Rect& from_rect);                // only part of it
  167.   void draw(const Rect& where_rect)        { draw(where_rect,(**pixmap).bounds); } // or the whole
  168.     
  169.   GrafPtr set_this_grafptr(void) const                // Set this grafptr and return the old one
  170.           { GrafPtr old_port; GetPort(&old_port); SetPort((GrafPtr)graf_world); return old_port; }
  171. public:
  172.   OffScreenBuffer(ScreenRect rect, const short clut_id=0);
  173.   ~OffScreenBuffer(void);
  174.  
  175.   PixMapHandle get_pixmap(void) const    { return pixmap; }
  176.         
  177.                                           // Bounding rect of the grafworld
  178.   ScreenRect q_bounds(void) const        { return (**pixmap).bounds; }
  179.   int height(void) const                { return _height; }         
  180.   int width(void) const                    { return _width; }         
  181.   int bytes_per_row(void) const            { return _bytes_per_row; }         
  182. };
  183.  
  184.                 // Make it easy setting and resetting
  185.                 // the current window
  186. class Set_NewGrafWorld
  187. {
  188.   CGrafPtr old_port;
  189.   GDHandle old_gdevice;
  190. public:
  191.                     // Switch to an off-screen grafworld
  192.   Set_NewGrafWorld(GWorldPtr graf_world)     
  193.     { GetGWorld(&old_port,&old_gdevice); SetGWorld((CGrafPtr)graf_world,nil); }
  194.                     // Restore the original grafworld
  195.  ~Set_NewGrafWorld(void) { SetGWorld(old_port,old_gdevice); }
  196. };
  197.  
  198. #endif
  199.